//
//  PlayerObject.m
//  Agility
//
//  Created by Dominique d'Argent on 16.03.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "PlayerObject.h"

#define VELOCITY 200.0f

@implementation PlayerObject

//
// spawnPlayer
//
// Spawns the player object and adds it to the game data dictionary.
//
+ (void)spawnPlayer {
    PlayerObject *player = [[self alloc] initWithImageNamed:GAME_PLAYER_KEY];
    player.position = CGPointMake(300, 500);
    
    [[GameData sharedGameData] addGameObject:player forKey:GAME_PLAYER_KEY];
    
    [player release];
}

#pragma mark - Object lifecycle

//
// initWithImageNamed:
//
// Creates a new player object.
//
- (id)initWithImageNamed:(NSString *)newImageName {
    self = [super initWithImageNamed:newImageName];
    
    if (self) {
        velocity = VELOCITY;
        
        [[GameData sharedGameData] addObserver:self 
                                    forKeyPath:@"touchPoint" 
                                       options:NSKeyValueObservingOptionNew 
                                       context:nil];
    }
    
    return self;
}

//
// dealloc
//
// Releases retained objects.
//
- (void)dealloc {
    [[GameData sharedGameData] removeObserver:self forKeyPath:@"touchPoint"];
    
    [super dealloc];
}

#pragma mark - Update mechanism

//
// updateWithValue:
//
// Updates properties based on the given value.
//
- (void)updateWithValue:(NSValue *)value {
    CGPoint touchPoint = [value CGPointValue];
    
    position = touchPoint;
    angle    = [self angleToPoint:position];
    
    durationForMovement = [self durationForMovementToPoint:position];
    durationForRotation = kGameObjectDefaultDuration;
    
    [super updateWithValue:value];
}

//
// updatePositionWithTimeInterval:
//
// Overriden to prevent automatic position calculation.
//
- (void)updatePositionWithTimeInterval:(NSTimeInterval)timeInterval {
}

// 
// updateTransformWithTimeInterval:
//
// Overridden to prevent automatic transform calculation.
//
- (void)updateTransformWithTimeInterval:(NSTimeInterval)timeInterval {
}

//
// didCollideWithGameObject:
//
// Posts a |GAME_OVER_NOTIFICATION| when the player object dies.
//
- (BOOL)didCollideWithGameObject:(GameObject *)collidingObject {
    [[NSNotificationCenter defaultCenter] postNotificationName:GAME_OVER_NOTIFICATION
                                                        object:gameObjectKey];
    
    return [super didCollideWithGameObject:collidingObject];
}

#pragma mark - KVO

//
// observeValueForKeyPath:ofObject:change:context:
//
// Dispatches KVO events. Called when the player taps on the screen.
//
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"touchPoint"]) {
        NSValue *value = [change valueForKey:NSKeyValueChangeNewKey];
        
        if (![value isEqual:[NSNull null]]) {
            CGPoint point;
            [value getValue:&point];
            
            [self updateWithValue:[NSValue valueWithCGPoint:point]];
        }
    }
}

@end
